<?php
class ymdian_dakuanControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$where=" 1=1 ";
		$url=APPADMIN."?m=ymdian_dakuan";
		$status=get('status','i');
		if($status){
			$where.=" AND status=$status";
			$url.="&status=$status";
		}
		$siteid=get('siteid','i');
			if($siteid){
				$where.=" AND siteid=".$siteid;
				$url.="&siteid=".$siteid;
			}
		$shopid=get('shopid','i');
		if($shopid){
			$where.=" AND shopid=".$shopid;
			$url.="&shopid=".$shopid;
		}
		$start_time=get('start_time','h');
			$end_time=get('end_time','h');
			if($start_time){
				$where.=" AND dateline>".strtotime($start_time)." ";
				$url.="&start_time=".$start_time;
			}
			
			if($end_time){
				$where.=" AND dateline<".strtotime($end_time)." ";
				$url.="&end_time=".$end_time;
			}
		$start=get('per_page','i');
		$limit=50;
		$option=array(
			"where"=>$where,
			"order"=>"id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("ymdian_dakuan")->select($option,$rscount);
		if($data){
			foreach($data as $v){
				$shopids[]=$v['shopid'];
				$siteids[]=$v['siteid'];
			}
			$shops=M("ymdian")->getShopByIds($shopids);
			$sites=M("sites")->getByIds($siteids);
			foreach($data as $k=>$v){
				$v['shop_name']=$shops[$v['shopid']]['title'];
				$v['site_name']=$sites[$v['siteid']]['sitename'];
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url); 
		$this->smarty->assign(array(
			"data"=>$data,
			"pagelist"=>$pagelist
		));
		$this->smarty->display("ymdian_dakuan/index.html");	
	}
	
	public function onAdd(){
		$id=get('id','i');
		$data=M("ymdian_dakuan")->selectRow("id=".$id);
		if($data){
			$site=M("sites")->selectRow("siteid=".$data['siteid']);
			$shop=M("ymdian")->selectRow("id=".$data['shopid']);
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"site"=>$site,
			"shop"=>$shop
		));
		
		$this->smarty->display("ymdian_dakuan/add.html");
	}
	
	public function onSave(){
		$id=get_post('id','i');
		$row=M("ymdian_dakuan")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID){
			$this->goAll("暂无权限",1);
		}
		if($row['status']==2){
			$this->goAll("暂不能修改");
		}
		$ispay=post('ispay','i');
		$payinfo=post('payinfo','h');
		if($ispay==2){
			if(empty($payinfo)){
				$this->goAll("请填写支付信息",1);
			}
		}
		M("ymdian_dakuan")->update(array(
			"ispay"=>$ispay,
			"payinfo"=>$payinfo
		),"id=".$id); 
		$this->goALl("保存成功");
	}
	
	public function onFinish(){
		$id=get_post('id','i');
		M("ymdian_dakuan")->update(array(
			"status"=>2
		),"id=".$id);
		$this->goALl("确认完成成功");
	}
	
	public function onCancel(){
		$id=get_post('id','i');
		M("ymdian_dakuan")->update(array(
			"status"=>4
		),"id=".$id);
		$this->goALl("取消成功");
	}
	
}
?>